package com.bbx.taxi.client.widget.Dailog;

import java.util.ArrayList;
import java.util.List;

import com.bbx.api.sdk.model.passanger.Return.WeightOrVolumn.WVDetails;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 */
public class MyWVDetailsDailog extends Dialog  implements
android.view.View.OnClickListener,OnItemClickListener {
	private String split = "\n";
	public LinearLayout layout_dailog;
	public TextView tv_title;
	public ListView listview;
	private boolean cancelable = true;
    private Context context;
    private boolean isFinish=false;
	public interface OnWVDetailsItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position,long id);
	}
	public OnWVDetailsItemClickListener mOnWVDetailsItemClickListener;
	public void setOnWVDetailsItemClickListener(OnWVDetailsItemClickListener hOnWVDetailsItemClickListener) {
		mOnWVDetailsItemClickListener = hOnWVDetailsItemClickListener;
	}
    
	public MyWVDetailsDailog(Context context) {
		super(context,(MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0)==0?R.style.Dialog_bocop:R.style.Dialog_bocop2);
		this.context=context;
		init();
	}

	private void init() {
		View contentView = View.inflate(getContext(), R.layout.pop_volume,
				null);
		setContentView(contentView);
		tv_title=(TextView)findViewById(R.id.tv_title);
		listview=(ListView)findViewById(R.id.listview);
		listview.setOnItemClickListener(this);
		layout_dailog = (LinearLayout) findViewById(R.id.layout_dailog);
		layout_dailog.setVisibility(View.GONE);
		this.setCancelable(true);
		this.setCanceledOnTouchOutside(true);
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
	}

	@Override
	public void show() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if(MessageDialog.map_dialog!=null){
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		MessageDialog.map_dialog.put(context.toString(), size+1);
		if(MessageDialog.map_dialog.get(context.toString())!=null&&MessageDialog.map_dialog.get(context.toString())<=MessageDialog.max_dialog){
			super.show();
			Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_in);
			layout_dailog.startAnimation(a);
			layout_dailog.setVisibility(View.VISIBLE);
			isFinish=false;
		}else{
			MessageDialog.map_dialog.put(context.toString(), MessageDialog.map_dialog.get(context.toString())-1);
		}
		}
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if(isFinish)
		super.dismiss();
		int size=MessageDialog.map_dialog.get(context.toString())!=null?MessageDialog.map_dialog.get(context.toString()):0;
		if(size>0){
			MessageDialog.map_dialog.put(context.toString(), size-1);
		}
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_out);
		layout_dailog.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation anim) {}
			
			@Override
			public void onAnimationRepeat(Animation anim) {}	
			@Override
			public void onAnimationEnd(Animation anim) {
				isFinish=true;
				dismiss();
			}
		});
		}

	}
	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}
	public void setTitle(String text) {
		tv_title.setText(text);
		
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_wait:
			dismiss();
			break;
		}
	}
	public void setAdapter(List<WVDetails> list) {
		MyAdapter mMyAdapter = new MyAdapter(context, list);
		listview.setAdapter(mMyAdapter);
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(mOnWVDetailsItemClickListener!=null)mOnWVDetailsItemClickListener.onItemClick(parent, view, position, id);
	}
	public class MyAdapter extends BaseAdapter {
		private List<WVDetails> list;
		private Context main;

		public MyAdapter(Context context, List<WVDetails> list) {
			this.list = list;
			main = context;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewholder;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater.from(main);
				convertView = (View) inflater
						.inflate(R.layout.item_goods, null);
				viewholder = new ViewHolder();
				viewholder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				viewholder.tv_content = (TextView) convertView
						.findViewById(R.id.tv_content);
				viewholder.layout_title = (LinearLayout) convertView
						.findViewById(R.id.layout_title);
				viewholder.tv_title2 = (TextView) convertView
						.findViewById(R.id.tv_title2);
				convertView.setTag(viewholder);
			} else
				viewholder = (ViewHolder) convertView.getTag();
			String content = list.get(position).getDesc();
			if (content != null) {
				if (content.contains(split)) {
					viewholder.layout_title.setVisibility(View.VISIBLE);
					viewholder.tv_title2.setVisibility(View.GONE);
					viewholder.tv_title.setText(content.split(split)[0]);
					viewholder.tv_content.setText(content.split(split)[1]);
				} else {
					viewholder.layout_title.setVisibility(View.GONE);
					viewholder.tv_title2.setVisibility(View.VISIBLE);
					viewholder.tv_title2.setText(content);
				}
			}
			return convertView;

		}

		public class ViewHolder {
			TextView tv_title, tv_title2, tv_content;
			LinearLayout layout_title;
		}
	}
}
